{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# What are Siamese Networks?"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Siamese networks are the special types of neural networks and it is one of the simplest and popularly used one shot learning algorithms. As we have learned in the previous chapter, one shot learning is a technique where we learn from only one training example per each class. So, the siamese network is predominantly used in applications where we don't have many data points for each of the class. For an instance, let us say we want to build a face recognition model for our organization and say about 500 people are working in our organization. If we want to build our face recognition model using convolutional neural network (CNN) from scratch then we need many images of all these 500 people for training the network and attaining good accuracy. But apparently, we will not have many images for all these 500 people and so it is not feasible to build a model using a CNN or any deep learning algorithm unless we have sufficient data points. So in these kinds of scenarios, we can resort to a sophisticated one shot learning algorithm like siamese network which can learn from less data points."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "But how do siamese networks work? Siamese networks basically consist of two symmetrical neural networks both sharing the same weights and architecture and both joined together at the end using some energy function $E$.  The objective of our siamese network is to learn whether the two inputs are similar or dissimilar.  Let's say we have two images $X_1$ and $X_2$ and we want to learn whether the two images are similar or dissimilar."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "\n",
    "As shown in the following diagram, we feed the image $X_1$ to network A and the image $X_2$ to another Network B. The role of both of these networks is to generate embeddings (feature vectors) for the input image. So we can use any network that will give us embeddings. Since our input is an image, we can use a convolutional network for generating the embeddings that is, for extracting features. Remember the role of CNN here is only to extract features and not to classify. As we know that these networks should have same weights and architecture, if our Network A is three-layer CNN then our Network B should also be a three-layer CNN and we have to use the same set of weights for both of these networks. So Network A and Network B will give us the embeddings for the input image $X_1$ and $X_2$ respectively. Then we will feed these embeddings to the energy function which tells us how similar the two input images are. Energy functions are basically any similarity measure such as Euclidean distance, cosine similarity, and so on."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "![title](Images/4.png)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Siamese networks are not only used for face recognition, but it is also used extensively in the applications where we don't have much data points and tasks where we need to learn a similarity between two inputs. The application of siamese network includes signature verification, similar question retrieval, object tracking and more. We will study siamese networks in detail in the upcoming section. "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We explore siamese networks in detail in the upcoming section. "
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python [default]",
   "language": "python",
   "name": "python2"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.11"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
